<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
                    <div class="row">
                        <div class="col-sm-12">
                             <div class="box">
                                <div class="box-header with-border">
                                    <div class=" col-sm-6">
                                        <?php echo form_open(current_url(), array('class' => 'form-inline', 'method'=>'get', 'id' => 'search_goods')); ?>                                    

                                        <div class="form-group" style="margin-right:15px;">
                                       
                                        <div class="input-group input-group-sm">
                                                <?php echo form_input($keyword);?>
                                               
                                        </div>                                        
                                        </div>
                                        <div class="form-group" >
                                            <?php echo form_button(array('type' => 'submit', 'class' => 'btn btn-sm btn-primary btn-flat', 'content' => "搜索")); ?>    
                                        </div>
                                        
                                        
                                        <?php echo form_close();?>
                                    </div>
                                    <h3 class="box-title pull-right" id="vultr"  style="color:#007bfc;fon-weight:bold;font-size:14px;line-height:30px;padding-right:20px;">
                                   
                                    </h3>
                                </div>
                                <div class="box-body">
                                       <?php if($data):?>
                                    <table class="table table-striped table-hover" style="margin-top:20px;">
                                        <thead>
                                            <tr>
                                                <th>ID</th>
                                                <th>IP地址</th>
                                                <th>磁盘空间</th>
                                                <th>用户名</th>  
                                                <th>密码</th> 
                                                <th>ROOT密码</th>     
                                                <th>备注</th>   
                                                <th>更新时间</th>                                        
                                                <th width="260">管理</th>
                                            </tr>
                                        </thead>
                                        <tbody>
<?php foreach ($data as $item):?>
<tr shopid="<?php echo $item["id"]; ?>">
    <td><?php echo $item["id"]; ?></td>
    <td ondblclick="edit(this,'ipaddr')"><?php echo $item["ipaddr"]; ?></td>
    <td><?php 
    if($item["diskused"])
    {
        $per=sprintf("%.2f", (floatval($item["diskused"])/floatval($item["disktotal"]))*100);
        if($per>90)
        {
            $color="text-red";
        }
        else
        {
            $color="text-green";
        }
        echo '<span class="'.$color.'">'.$item["diskused"]." / ".$item["disktotal"]." (".$per."%)</span>";
    }
    else
    {
    	echo "尚未更新";
    }
	 ?></td>
     
    <td>
      <input type="text" readonly="true" value="<?php echo $item["username"]; ?>" class="form-control input-sm" id="key<?php echo $item["id"]; ?>1" style="display:inline;width:100px;font-size:12px;vertical-align:middle;"><button data-clipboard-target="#key<?php echo $item["id"]; ?>1" class="btn btn-sm btn-warning btn-flat copybtn" >复制</button>
    </td>
    <td>
    <input type="text" readonly="true" value="<?php echo $item["pass"]; ?>" class="form-control input-sm" id="key<?php echo $item["id"]; ?>2" style="display:inline;width:100px;font-size:12px;vertical-align:middle;"><button data-clipboard-target="#key<?php echo $item["id"]; ?>2" class="btn btn-sm btn-warning btn-flat copybtn">复制</button>
    </td>
    <td>
    <input type="text" readonly="true" value="<?php echo $item["rootpass"]; ?>" class="form-control input-sm" id="key<?php echo $item["id"]; ?>3" style="display:inline;width:130px;font-size:12px;vertical-align:middle;"><button data-clipboard-target="#key<?php echo $item["id"]; ?>3" class="btn btn-sm btn-warning btn-flat copybtn">复制</button>
    </td>
    <td ondblclick="edit(this,'memo')"><?php echo $item["memo"]?$item["memo"]:"双击修改"; ?></td>
    <td><?php echo $item["lastupdate"]!="0000-00-00 00:00:00"?$item["lastupdate"]:"尚未更新"; ?></td>
   
    <td>
 
    <?php echo anchor('backend/server/sites/'.$item["id"],'<i class="fa fa-cloud fa-lg"></i> 网站','class="btn btn-xs btn-info"'); ?>&nbsp&nbsp
    <button class="btn btn-xs bg-maroon" onclick="modify('<?php echo $item["ipaddr"]; ?>',<?php echo $item["id"];?>);"><i class="fa fa-edit fa-lg"></i> 配置</button>

    <?php
        if($item["bturl"])
        {
            echo  "&nbsp&nbsp".anchor($item["bturl"],'<i class="fa fa-send fa-lg"></i> 管理','class="btn btn-xs btn-primary" target="_blank"'); 
        }

     
    ?>
 
    </td>
</tr>
<?php endforeach;?>
                                        </tbody>
                                    </table>
<?php endif;?>

                                    <?php 
                                    if(empty($data)){
                                        echo "<h2 style='text-align:center;padding:50px 0;'>没有查询到数据</h2>";
                                    }
                                    ?>
                                    <?php echo $pager;?>

                                    <div class=" pull-right form-inline" style="line-height:30px;margin-top:25px;">
                                        <?php echo '共'.$total.'条产品，'.$page.'/'.$pages.'页';?> 
                                    </div>

                                </div>
                            </div>
                         </div>
                    </div>
        


        <?php echo form_open(current_url(), array('class' => 'form-horizontal','onsubmit'=>'return checkform();', 'id' => 'form-create_notice')); ?>
        <div class="row">
             <div class="col-sm-12">
                <div class="box">
       
                 <div class="box-header with-border">
                 <h3 class="box-title">添加服务器</h3>
                 </div>

                 <div class="box-body">

                    <div class="row">
                        <div class="col-xs-3">
                            <input type="text" class="form-control" name="ipaddr" id="ipaddr" placeholder="服务器IP">
                        </div>

                        <div class="col-xs-3">
                            <input type="text" class="form-control" name="bturl" id="bturl" placeholder="宝塔地址">
                        </div>

                        <div class="col-xs-3">
                            <input type="text" class="form-control" name="username" id="username" placeholder="宝塔用户名">
                        </div>

                        <div class="col-xs-3">
                            <input type="text" class="form-control" name="pass" id="pass" placeholder="宝塔密码">
                        </div>
                    </div>
                    <div class="row" style="margin-top:10px;">
                        <div class="col-xs-6">
                            <input type="text" class="form-control" name="memo" id="memo" placeholder="备注">
                        </div>
                        <div class="col-xs-3">
                            <input type="text" class="form-control" name="rootpass" id="rootpass" placeholder="ROOT密码">
                        </div>
                    </div>
                  
                    <div class="row" style="margin-top:10px;">
                        <div class="col-xs-3">
                            <input type="hidden" name="action" value="add">
                            <input type="submit" class="btn btn-flat btn-primary btn-sm" value="添加网站">
                        </div>
                       
                    </div> 

                 </div>
          
              
                </div>
              </div>
            <!-- /.box-body -->
          </div>
        <?php echo form_close();?>
<script>

$(function(){
    require(['jquery','clipboard.min'], function($,clipboardlib){
        var clipboard = new clipboardlib('.copybtn');
        clipboard.on('success', function(e) {
            layer.msg("复制成功");
        });
        clipboard.on('error', function(e) {
            layer.msg("复制失败");
        });
    });
    <?php
    if ($config["vultr"]):
    ?>
    $("#vultr").load("/backend/server/vultr");
    <?php 
    endif;
    ?>
});

    var atedit=0;
    function edit(obj,attrName){
        if (atedit==1)
        return;
        else
        atedit=1;
        var oldHtml = obj.innerHTML;
        var newobj = document.createElement('input');
        newobj.name = attrName;
        newobj.type = "text";
        newobj.setAttribute("class","form-control input-sm");
        if($.trim(oldHtml)!="双击修改"){
            newobj.value = oldHtml;
        }
        var cid = $(obj).parent().attr("shopid");     
        obj.innerHTML = '';  
        obj.appendChild(newobj);
        newobj.focus(); 

        $(newobj).keydown(function (event) {
            if (event.keyCode == 13) {
                $(this).trigger("blur");
            }
        });

        $(newobj).blur(function(){
            atedit=0;            
            if(this.value==null || this.value==""){
                obj.innerHTML = oldHtml;
                return false;
            }
            obj.innerHTML = this.value?this.value:oldHtml;       
            if(oldHtml != this.value){
                $.ajax({
                    type:"post",
                    url:"/backend/server/ajaxedit",
                    dataType:"json",
                    data:{"attrName":attrName,"newHtml":this.value,"cid":cid},
                    error:function(){
                    },
                    success:function(){
                    },
                });
            }  

        })
    }

    var modify=function(title,oid){
        layer.open({
        type: 2,
        title: title+' 修改配置',
        shadeClose: true,
        shade: 0.8,
        area: ['74%', '70%'],
        content: '/backend/server/modify/'+oid
        }); 
    }


    var checkform = function(){
        checkfild={
        "ipaddr":"IP地址",
        };
        for (var index in checkfild) {
            if($("input[name='"+index+"']").val().trim().length==0)
            {
                layer.msg(checkfild[index]+"不能为空");
                return false;
            }
        }
    };

</script>
